﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using LinkCard.Utils;
using LinkCard.Dao;
using LinkCard.Model;
using Library.Components;
using System.Configuration;

namespace LinkCard.CustomerView
{
    public partial class Register : System.Web.UI.Page
    {
        CString objStr = new CString();
        protected void Page_Load(object sender, EventArgs e)
        {

            # region .Random string
            string sRandom = objStr.RandomString(5, false, false);
            Session["SecuritySession"] = sRandom;
            txtNewCode.Value = sRandom;
            # endregion

            Page.Title = "Đăng kí - LinkCard";
            Page.MetaDescription = "Đăng kí - LinkCard";

        }
        //data: "{'sFullname':'" + sFullname
        //                    + "','sBirthday':'" + sBirthday
        //                    + "','sGioitinh':'" + sGioiTinh
        //                    + "','sIDNumber':'" + sIDNumber
        //                    + "','sIDPublishDate':'" + sIDPublishDate
        //                    + "','sMobileNumber':'" + sMobileNumber
        //                    + "','sEmail':'" + sEmail
        //                    + "','sAddress':'" + sAddress
        //                    + "','sCityID':'" + sCityID
        //                    + "','sFavGift':'" + sFavGift
        //                    + "','sJob':'" + sJob
        //                    + "','sGLP':'" + sGLP
        //                    + "','sCar':'" + sCar
        //                    + "','sInsurance':'" + sInsurance
        //                    + "','sKnowLinkCard':'" + sKnowLinkCard
        //                    + "','sReceiverGift':'" + sReceiverGift
        //                    + "','sReceiverSMS':'" + sReceiverSMS + "'}",
        [System.Web.Services.WebMethod]
        public static string CreateNew(string sFullname, string sBirthday, string sGioitinh, string sIDNumber, string sIDPublishDate,
            string sMobileNumber, string sEmail, string sAddress, string sCityID, string sFavGift, string sJob, string sGLP, string sCar,
            string sInsurance, string sKnowLinkCard, string sReceiverGift, string sReceiverSMS)
        {
            System.Globalization.DateTimeFormatInfo dateInfo = new System.Globalization.DateTimeFormatInfo();
            dateInfo.ShortDatePattern = "dd/MM/yyyy";
            try
            {
                if (checkExist(sEmail, sMobileNumber)) { return "-1"; }
                else
                {
                    try
                    {
                        var dao = new CustomerDao();
                        var daoTemp = new CustomerTempDao();

                        DateTime? _birthday = DateTime.Now;
                        DateTime? _IDPublishDate = DateTime.Now;
                        _birthday = Convert.ToDateTime(sBirthday, dateInfo);
                        _IDPublishDate = Convert.ToDateTime(sIDPublishDate, dateInfo);
                        
                        byte _gioitinh = 0;
                        if (sGioitinh == "nam")
                            _gioitinh = 1;

                        var _favgift = true;
                        var _receiverGift = true;
                        var _receiverSMS = true;
                        byte _isGLP = 0;

                        if (sFavGift == "0")
                            _favgift = false;
                        if (sReceiverGift == "0")
                            _receiverGift = false;
                        if (sReceiverSMS == "0")
                            _receiverSMS = false;
                        if (sGLP == "")
                            _isGLP = 1;
                        
                        var _guidid = Guid.NewGuid().ToString();
            //            (string sFullname, string sBirthday, string sGioitinh, string sIDNumber, string sIDPublishDate,
            //string sMobileNumber, string sEmail, string sAddress, string sCityID, string sFavGift, string sJob, string sGLP, string sCar,
            //string sInsurance, string sKnowLinkCard, string sReceiverGift, string sReceiverSMS)
                        var _cus = new Customer
                                       {
                                           Fullname = sFullname,
                                           Birthday = _birthday,
                                           Gender = _gioitinh,
                                           ID = sIDNumber,
                                           IDPublishDate = _IDPublishDate,
                                           MobilePhone = sMobileNumber,
                                           Email = sEmail,
                                           Address = sAddress,
                                           CityID = Convert.ToInt32(sCityID),
                                           FavGift = _favgift,
                                           Job = sJob,
                                           VietnamAirlineGLPNumber = sGLP,
                                           VietnamAirlineGLP = _isGLP,
                                           Car = sCar,
                                           Insurance = sInsurance,
                                           KnowLinkCard = Convert.ToByte(sKnowLinkCard),
                                           ReceiverGift = _receiverGift,
                                           ReceiverSMS = _receiverSMS,
                                           RegisterDate = DateTime.Now,
                                           GUID = _guidid,
                                           GUIDCreateDate = DateTime.Now,
                                           Point = 0,
                                           SystemRegister = 0,
                                           Status = 0,
                                           CardLevel = 1,
                                           IsActive = false
                                       };
                        dao.Add(_cus);

                        var _cusTemp = new CustomerTemp
                        {
                            Fullname = sFullname,
                            Birthday = _birthday,
                            Gender = _gioitinh,
                            ID = sIDNumber,
                            IDPublishDate = _IDPublishDate,
                            MobilePhone = sMobileNumber,
                            Email = sEmail,
                            Address = sAddress,
                            CityID = Convert.ToInt32(sCityID),
                            FavGift = _favgift,
                            Job = sJob,
                            VietnamAirlineGLPNumber = sGLP,
                            VietnamAirlineGLP = _isGLP,
                            Car = sCar,
                            Insurance = sInsurance,
                            KnowLinkCard = Convert.ToByte(sKnowLinkCard),
                            ReceiverGift = _receiverGift,
                            ReceiverSMS = _receiverSMS,
                            RegisterDate = DateTime.Now,
                            GUID = _guidid,
                            GUIDCreateDate = DateTime.Now,
                            Point = 0,
                            SystemRegister = 0,
                            Status = 1,
                            CardLevel = 1,
                            IsActive = false,
                            CustomerID = _cus.CustomerID
                        };
                        daoTemp.Add(_cusTemp);

                        //Send email
                        LinkCard.Dao.NewsLetterDao.SendRegisterEmail(sEmail, _guidid, sMobileNumber, _cus.CustomerID.ToString());
                        //Send SMS
                        string mobileInform = ConfigurationManager.AppSettings["RegisterInformMobile"];

                        Int64 _kqSMS = Utils.SMSTool.SendSMS(mobileInform, "Khach hang: " + sEmail + ", sdt: " + sMobileNumber + " da dang ki online vao luc: " + DateTime.Now);
                        if (_kqSMS < 0) //nếu nhắn tin lỗi thì log lại lỗi, nhắn ok thì thôi ko lưu lại nữa
                        {
                            var logSMS = LogActions.LogItem(0, 0, 0, 0, 0, "Lỗi nhắn tin: " + _kqSMS.ToString(), "Register", "Online");
                        }
                        return _cus.CustomerID.ToString();
                    }
                    catch
                    {
                        return "-2";
                    }

                }
            }
            catch
            {
                return "-2";
            }
        }

        public static bool checkExist(string sEmail, string sPhone)
        {
            var dt = new DataTable();
            dt = DataHelper.SelectTable("Select CustomerID from Customer where Email ='" + hash1way.SQLPatch(sEmail) + "' or MobilePhone ='" + hash1way.SQLPatch(sPhone) + "'", false);
            if (dt.Rows.Count > 0) return true;
            else return false;
        }
    }
}